project(mscordbi)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CLR_CMAKE_RUNTIME_MONO 1)
set(CLR_DIR ${CLR_REPO_ROOT_DIR}/src/coreclr)
set(CMAKE_OSX_ARCHITECTURES ${CMAKE_SYSTEM_PROCESSOR})
set(CMAKE_EXE_LINKER_FLAGS_CHECKED "")
set(CMAKE_SHARED_LINKER_FLAGS_CHECKED "")
set(CLR_CMAKE_HOST_ARCH ${CMAKE_GENERATOR_PLATFORM})
set(FEATURE_EVENT_TRACE 0)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_definitions(-fvisibility-global-new-delete-hidden)
endif()

if(HOST_WIN32)
    if(HOST_X86)
        set(CLR_CMAKE_HOST_ARCH x86)
    elseif(HOST_ARM64)
        set(CLR_CMAKE_HOST_ARCH arm64)
    elseif(HOST_ARM)
        set(CLR_CMAKE_HOST_ARCH arm)
    elseif(HOST_AMD64)
        set(CLR_CMAKE_HOST_ARCH x64)
    endif()
endif()

add_definitions(-DDBI_COMPONENT_MONO)

include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}/../../..
  ${PROJECT_SOURCE_DIR}/../../
  ${PROJECT_SOURCE_DIR}/../mscordbi
  ${PROJECT_SOURCE_DIR}/../mscordbi/socket-dbi
  ${CLR_DIR}/md/enc
  ${CLR_DIR}/inc
  ${CLR_DIR}/pal/inc
  ${CLR_DIR}/md/inc
  ${CLR_DIR}/md/compiler
  ${CLR_DIR}/pal/prebuilt/inc
  ${CLR_DIR}/nativeresources)

set(mscorbi_sources_base
    cordb.cpp
    cordb.h
    cordb-appdomain.cpp
    cordb-appdomain.h
    cordb-assembly.cpp
    cordb-assembly.h
    cordb-blocking-obj.cpp
    cordb-blocking-obj.h
    cordb-breakpoint.cpp
    cordb-breakpoint.h
    cordb-chain.cpp
    cordb-chain.h
    cordb-class.cpp
    cordb-class.h
    cordb-code.cpp
    cordb-code.h
    cordb-eval.cpp
    cordb-eval.h
    cordb-frame.cpp
    cordb-frame.h
    cordb-function.cpp
    cordb-function.h
    cordb-process.cpp
    cordb-process.h
    cordb-register.cpp
    cordb-register.h
    cordb-stackwalk.cpp
    cordb-stackwalk.h
    cordb-stepper.cpp
    cordb-stepper.h
    cordb-thread.cpp
    cordb-thread.h
    cordb-type.cpp
    cordb-type.h
    cordb-value.cpp
    cordb-value.h
)

addprefix(mscorbi_sources ../mscordbi/ "${mscorbi_sources_base}")

if(HOST_DARWIN)
set(OS_LIBS "-framework CoreFoundation" "-framework Foundation")
elseif(HOST_LINUX)
set(OS_LIBS pthread m dl)
elseif(HOST_WIN32)
set(OS_LIBS bcrypt.lib Mswsock.lib ws2_32.lib psapi.lib version.lib advapi32.lib winmm.lib kernel32.lib)
set(mscorbi_sources "${mscorbi_sources};${VERSION_FILE_RC_PATH}") # this is generated by GenerateMonoVersionFile in mono.proj
endif()

add_subdirectory(${PROJECT_SOURCE_DIR}/socket-dbi)

include(${CLR_ENG_NATIVE_DIR}/configurecompiler.cmake)

if (CLR_CMAKE_HOST_UNIX)
  # Add custom targets that the pal build expects.
  add_component(${CMAKE_INSTALL_DEFAULT_COMPONENT_NAME})
  add_component(paltests_install)
  include_directories(${CLR_DIR}/pal/inc)
  include_directories(${CLR_DIR}/pal/inc/rt)
  include_directories(${CLR_DIR}/pal/src/safecrt)

  append("-Wno-missing-prototypes  -Wno-pointer-arith -Wno-macro-redefined" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  add_subdirectory(${CLR_DIR}/pal pal)

  include_directories(${CLR_DIR}/pal/inc/rt/cpp)
  add_compile_options(-nostdinc)
endif (CLR_CMAKE_HOST_UNIX)

if (CLR_CMAKE_HOST_UNIX)
    add_subdirectory(${CLR_DIR}/nativeresources nativeresources)
endif()

add_subdirectory(${CLR_DIR}/md/runtime md/runtime)
add_subdirectory(${CLR_DIR}/md/compiler md/compiler)

include(${CLR_DIR}/clrdefinitions.cmake)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/../../)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/../../inc/)
include_directories(${CLR_DIR}/minipal)

add_subdirectory(${CLR_DIR}/md/enc md/enc)
add_subdirectory(${CLR_DIR}/utilcode utilcode)
if (CLR_CMAKE_HOST_UNIX)
    add_subdirectory(${CLR_DIR}/palrt palrt)
    append("-Wno-strict-prototypes -Wno-deprecated -Wno-pointer-arith" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
endif (CLR_CMAKE_HOST_UNIX)

add_library(mscordbi SHARED "${mscorbi_sources};${PROJECT_SOURCE_DIR}/../../mono/component/debugger-protocol.c;${PROJECT_SOURCE_DIR}/../../../coreclr/pal/prebuilt/idl/xcordebug_i.cpp;${PROJECT_SOURCE_DIR}/../../../coreclr/pal/prebuilt/idl/cordebug_i.cpp")

#SET(CMAKE_C_COMPILER ${CMAKE_CXX_COMPILER})

set_source_files_properties(${PROJECT_SOURCE_DIR}/../../mono/component/debugger-protocol.c PROPERTIES LANGUAGE CXX)

set(START_WHOLE_ARCHIVE -Wl,--whole-archive)
set(END_WHOLE_ARCHIVE -Wl,--no-whole-archive)
# IMPORTANT! Please do not rearrange the order of the libraries. The linker on Linux is
# order dependent and changing the order can result in undefined symbols in the shared
# library.
set(COREDBI_LIBRARIES
    mdruntimerw-dbi
    utilcodestaticnohost
    mdruntime-dbi
    mdcompiler-dbi
    socket-dbi
    ${OS_LIBS}
)
if(CLR_CMAKE_HOST_UNIX)
    list(APPEND COREDBI_LIBRARIES
        ${START_WHOLE_ARCHIVE}
        palrt
        coreclrpal
        ${END_WHOLE_ARCHIVE}
        nativeresourcestring
    )
endif()

target_link_libraries(mscordbi PRIVATE ${COREDBI_LIBRARIES} monoapi)
install_with_stripped_symbols(mscordbi TARGETS lib)
